package com.ysr.controller;

import com.ysr.pojo.PayMent;
import com.ysr.result.CommonResult;
import com.ysr.service.PayMentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/payment")
@Slf4j
public class PayMentController {
    @Autowired
    private PayMentService payMentService;
    @Value("${server.port}")
    private String serverPort;

    //注入服务发现的注解
    @Autowired
    private DiscoveryClient discoveryClient;

    //获取服务信息
    @GetMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String s : services){
            log.info("********注册到eureka中的服务中有:"+services);
        }
        List <ServiceInstance> instances = discoveryClient.getInstances("ysr-payment");
        for (ServiceInstance s: instances) {
            log.info("当前服务的实例有"+s.getServiceId()+"\t"+s.getHost()+"\t"+s.getPort()+"\t"+s.getUri());
        }
        return this.discoveryClient;
    }

    @PostMapping("/create")
    public CommonResult create(@RequestBody PayMent payMent){
        int i = payMentService.create(payMent);
        log.info("***************插入成功*******"+i);
        if(i>0){
            return new CommonResult(200,"插入数据库成功",i);
        }else{
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult queryById(@PathVariable("id") Long id){
        PayMent payMent = payMentService.queryById(id);
        log.info("***************查询成功*********"+payMent);
        if(payMent!=null){
            return new CommonResult(200,"查询成功8001",payMent);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody PayMent payMent){
        payMentService.add(payMent);
        log.info("***************插入成功*******");
        return new CommonResult(200,"插入数据库成功");
    }

    @GetMapping("/queryPayMentById")
    public CommonResult queryPayMentById(Long id){
        PayMent payMent = payMentService.queryPayMentById(id);
        log.info("***************查询成功*********"+payMent);
        if(payMent!=null){
            return new CommonResult(200,"查询成功8001",payMent);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    //模拟业务接口延时3秒
    @GetMapping("/feign/timeout")
    public String PaymentFeignTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return serverPort;
    }

    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = payMentService.paymentInfo_OK(id);
        log.info("*******************result:" + result);
        return result;
    }

    /**
     * 超时访问
     *
     * @param id
     * @return
     */
    @GetMapping("/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String result = payMentService.paymentInfo_TimeOut(id);
        log.info("*********************result:" + result);
        return result;

    }
}
